//
//  HMMusicsViewController.m
//  04-黑马音乐
//
//  Created by apple on 15-3-29.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "HMMusicsViewController.h"
#import "HMMusic.h"
#import "UIImage+Circle.h"
#import "HMPlayingViewController.h"
#import "HMMusicTool.h"

@interface HMMusicsViewController ()

@property (nonatomic, strong) NSArray *musics;

@property (nonatomic, strong) HMPlayingViewController *playingVc;

@end

@implementation HMMusicsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 80;
   }

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.musics.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"MusicCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    // 1.取出模型数据
    HMMusic *music = self.musics[indexPath.row];
    
    // 2.给cell设置数据
    cell.imageView.image = [UIImage circleImageWithName:music.singerIcon borderWidth:3.0 borderColor:[UIColor purpleColor]];
    cell.textLabel.text = music.name;
    cell.detailTextLabel.text = music.singer;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 0.让cell不被选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 1.取出模型数据
    HMMusic *music = self.musics[indexPath.row];
    
    // 2.设置正在播放的歌曲
    [HMMusicTool setPlayingMusic:music];
    
    // 3.弹出播放控制器
    [self.playingVc show];
}

#pragma mark - 懒加载代码
- (NSArray *)musics
{
    if (_musics == nil) {
        _musics = [HMMusicTool musics];
    }
    return _musics;
}

- (HMPlayingViewController *)playingVc
{
    if (_playingVc == nil) {
        _playingVc = [[HMPlayingViewController alloc] init];
    }
    return _playingVc;
}

@end
